1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.math.*;
32
33 public class PowTests {
34 static int zeroAndOneTests() {
35 int failures = 0;
36
37 BigDecimal[][] testCases = {
38 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
39 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MAX_VALUE)},
40 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MAX_VALUE)},
41 {BigDecimal.valueOf(0, Integer.MAX_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MAX_VALUE)},
42
43 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
44 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(0, Integer.MIN_VALUE)},
45 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(2), BigDecimal.valueOf(0, Integer.MIN_VALUE)},
46 {BigDecimal.valueOf(0, Integer.MIN_VALUE), new BigDecimal(999999999), BigDecimal.valueOf(0, Integer.MIN_VALUE)},
47
48 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
49 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MAX_VALUE)},
50 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(2), null},
51 {BigDecimal.valueOf(1, Integer.MAX_VALUE), new BigDecimal(999999999), null},
52
53 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(0), BigDecimal.valueOf(1, 0)},
54 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(1), BigDecimal.valueOf(1, Integer.MIN_VALUE)},
55 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(2), null},
56 {BigDecimal.valueOf(1, Integer.MIN_VALUE), new BigDecimal(999999999), null},
57 };
58
59 for(BigDecimal[] testCase: testCases) {
60 int exponent = testCase[1].intValueExact();
61 BigDecimal result;
62
63 try{
64 result = testCase[0].pow(exponent);
65 if (!result.equals(testCase[2]) ) {
66 failures++;
67 System.err.println("Unexpected result while raising " +
68 testCase[0] +
69 " to the " + exponent + " power; expected " +
70 testCase[2] + ", got " + result + ".");
71 }
72 } catch (ArithmeticException e) {
73 if (testCase[2] != null) {
74 failures++;
75 System.err.println("Unexpected exception while raising " + testCase[0] +
76 " to the " + exponent + " power.");
77
78 }
79 }
80 }
81
82 return failures;
83 }
84
85 public static void main(String argv[]) {
86 int failures = 0;
87
88 failures += zeroAndOneTests();
89
90 if (failures > 0) {
91 throw new RuntimeException("Incurred " + failures +
92 " failures while testing pow methods.");
93 }
94 }
95
96 }